package com.kmproject.haifu.mvp.ui.adapter;


import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.ui.activity.HouseRentShowInfoActivity;
import com.library.flowlayout.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class NyCollectHouseRentAdapter extends BaseQuickAdapter<HouseRentListShowInfoBean, BaseViewHolder> {
    private boolean isShowCheck = false;//选择开关
    private int isselectAll = 0;//0表示既不是选择全部，也不是全部不选，1表示全选中  2表示全不选
    private ImageLoader glideImage;

    public NyCollectHouseRentAdapter(@Nullable List<HouseRentListShowInfoBean> data) {
        super(R.layout.house_me_list_item, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, HouseRentListShowInfoBean item) {
          if (item.getIsExpired()==1)
          {
              //已过期失效
              helper.<LinearLayout>getView(R.id.mystore_head_renthouse_item_wzs_lay).setVisibility(View.VISIBLE);
          }
        if (glideImage == null) {
            glideImage = ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
        if (isShowCheck) {
            if (isselectAll == 1) {
                helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setChecked(true);
                item.setHaveSelect(true);
            } else if (isselectAll == 2) {
                helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setChecked(false);
                item.setHaveSelect(false);
            }else {
                helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setChecked(item.isHaveSelect());
            }
            helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setVisibility(View.VISIBLE);
            helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    item.setHaveSelect(isChecked);

                }
            });
        } else {
            helper.<CheckBox>getView(R.id.houseRentingShow_item_selectImg).setVisibility(View.GONE);
            item.setHaveSelect(false);
        }
        //判断是否关联了店铺
        if (item.getUserStoreName()!=null&&!TextUtils.isEmpty(item.getUserStoreName()))
        {
            //关联了店铺
            helper.<LinearLayout>getView(R.id.houserenting_main_content_item_store_lay).setVisibility(View.VISIBLE);
            helper.<TextView>getView(R.id.houserenting_main_content_item_store_name).setText(item.getUserStoreName());
        }else {
            //未关联店铺
            helper.<LinearLayout>getView(R.id.houserenting_main_content_item_store_lay).setVisibility(View.GONE);
        }
        if (item.getHouseImageList()!=null&&!item.getHouseImageList().isEmpty())
        {
            glideImage.loadImage(mContext, ImageConfigImpl.builder().url(item.getHouseImageList().get(0).getImageUrl()).imageRadius(5)
                    .imageView(helper.<ImageView>getView(R.id.houserenting_main_content_item_img))
                    .build());
        }


        String temName = "";
        if (item.getHouseAreaName() != null && !item.getHouseAreaName().isEmpty()) {

            for (int i = 0; i < item.getHouseAreaName().size(); i++) {
                if (i != item.getHouseAreaName().size() - 1) {
                    temName += item.getHouseAreaName().get(i).getName() + ".";
                } else {
                    temName += item.getHouseAreaName().get(i).getName();
                }
            }
        }
        helper.getView(R.id.houseRentingShow_item_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getIsExpired()==1)
                {
                    //已经失效的
                     ArmsUtils.makeText(mContext,"房源已失效！");
                    return;
                }
                Intent intent=new Intent(mContext, HouseRentShowInfoActivity.class);
                intent.putExtra("rentId",item.getRentId());
                intent.putExtra("type","userCenterInfo");
                intent.putExtra("lgt","0");
                intent.putExtra("lat","0");
                ArmsUtils.startActivity(intent);
            }
        });
        if (!item.getRentType().equals("dz"))
        {
            helper.<TextView>getView(R.id.houserenting_main_content_item_unit).setText("/月");
        } else
        {
            helper.<TextView>getView(R.id.houserenting_main_content_item_unit).setText("/天");
        }
        HouseRentingShowHeZuYqItemAdapter adapter;
        List<HouseRentListShowBean.DetailsBean> detailsBeans=new ArrayList<>();
        if ("jmzz".equals(item.getHouseCategory()))
        {
            //居民住宅
            helper.<TextView>getView(R.id.houserenting_main_content_item_txt1).setText(item.getRentTypeName()+"."+item.getRoomCountName()+item.getToiletCountName()+item.getDrawingCountName()+"."+item.getHouseName());
            helper.<TextView>getView(R.id.houserenting_main_content_item_txt2).setText(item.getAcreage()+"m² "+item.getFloorName().replace("层","") + "/" + item.getTotalFloorName()+" "+temName);
            helper.<TextView>getView(R.id.houserenting_main_content_item_price).setText(item.getFormatMonthRent()+"P");
            if (item.getRentType().equals("zz")||item.getRentType().equals("hz"))
            {
                //整租合租  显示前三个亮点
                if (item.getDetails()!=null&&!item.getDetails().isEmpty())
                {
                    if (item.getDetails().size()>2)
                    {
                        detailsBeans.addAll(item.getDetails().subList(0,3));
                    }else {
                        detailsBeans.addAll(item.getDetails());
                    }
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.VISIBLE);
                }else {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.GONE);
                }

            }else if (item.getRentType().equals("bs"))
            {
                //别墅 别墅显示装修和两个亮点
                if (item.getDecorationTypeName().contains("精")||item.getDecorationTypeName().contains("豪"))
                {
                    detailsBeans.add(new HouseRentListShowBean.DetailsBean(item.getDecorationTypeName()));
                }
                if (item.getDetails()!=null&&!item.getDetails().isEmpty())
                {
                    if (item.getDetails().size()>1)
                    {
                        detailsBeans.addAll(item.getDetails().subList(0,2));
                    }else {
                        detailsBeans.addAll(item.getDetails());
                    }
                }
                if (detailsBeans.isEmpty())
                {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.GONE);
                }else {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.VISIBLE);
                }

            }else {
                //短租 显示房屋类型和两个房屋亮点
                detailsBeans.add(new HouseRentListShowBean.DetailsBean(item.getRentRoomName()));
                if (item.getDetails()!=null&&!item.getDetails().isEmpty())
                {
                    if (item.getDetails().size()>1)
                    {
                        detailsBeans.addAll(item.getDetails().subList(0,2));
                    }else {
                        detailsBeans.addAll(item.getDetails());
                    }
                }
                if (detailsBeans.isEmpty())
                {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.GONE);
                }else {
                    helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setVisibility(View.VISIBLE);
                }
                //  rentRoomName
            }
            adapter=  new HouseRentingShowHeZuYqItemAdapter(detailsBeans);
            helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setLayoutManager(new GridLayoutManager(mContext, 3));
            helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).addItemDecoration(new SpaceItemDecoration(AppUtil.dp2px(2)));
            helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setAdapter(adapter);

            return;
        }
        //商业用地
        helper.<TextView>getView(R.id.houserenting_main_content_item_txt1).setText(item.getHouseName());
        helper.<TextView>getView(R.id.houserenting_main_content_item_txt2).setText(item.getAcreage()+"m² "+temName);
        helper.<TextView>getView(R.id.houserenting_main_content_item_price).setText(String.valueOf(item.getMonthRent().intValue())+"P");
        if ("sp".equals(item.getRentType()))
        {
            //商铺
            if (item.getTransferOrLease().equals("transfer"))
            {
                detailsBeans.add(new HouseRentListShowBean.DetailsBean("商铺转让"));
            }else {
                detailsBeans.add(new HouseRentListShowBean.DetailsBean("商铺出租"));
            }
            if (item.getDailyRent().doubleValue()>0)
            {
                detailsBeans.add(new HouseRentListShowBean.DetailsBean("短租"));
            }
        }else {
            //仓库，写字楼，办公室
            detailsBeans.add(new HouseRentListShowBean.DetailsBean(item.getRentTypeName()));
            if (item.getDailyRent().doubleValue()>0)
            {
                detailsBeans.add(new HouseRentListShowBean.DetailsBean("短租"));
            }
            if ("bgs".equals(item.getRentType()))
            {
                //办公室
                if (item.getIsSeparable()==1)
                {
                    //是否可分割
                    detailsBeans.add(new HouseRentListShowBean.DetailsBean("可分割"));
                }
                if (item.getIsSeparable()==1)
                {
                    //是否可分割
                    detailsBeans.add(new HouseRentListShowBean.DetailsBean("带办公家具"));
                }
            }
        }
        helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setLayoutManager(new GridLayoutManager(mContext, 3));
        if (helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).getItemDecorationCount()==0){
            helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).addItemDecoration(new SpaceItemDecoration(AppUtil.dp2px(2)),0);
        }
        helper.<RecyclerView>getView(R.id.houserenting_main_content_item_RecyclerView).setAdapter(new HouseRentingShowHeZuYqItemAdapter(item.getDetails()));

    }



    /**
     *是否选中全部
     * @param isselectAll
     */
    public void setIsselectAll(int isselectAll) {
        this.isselectAll = isselectAll;
        notifyDataSetChanged();
    }



    /**
     * 控制是否显示删除
     * @param showCheck
     */
    public void setShowCheck(boolean showCheck) {
        isShowCheck = showCheck;
        notifyDataSetChanged();
    }
}
